package com.dwd.pet.controller;

import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import com.dwd.basic.utils.SystemContext;
import com.dwd.org.mapper.ShopMapper;
import com.dwd.pet.pojo.Pet;
import com.dwd.pet.pojo.SearchMasterMsg;
import com.dwd.pet.query.PetQuery;
import com.dwd.pet.query.SearchMasterMsgQuery;
import com.dwd.pet.service.IPetService;
import com.dwd.pet.service.ISearchMasterMsgService;
import com.dwd.servicemanager.pojo.Product;
import com.dwd.servicemanager.query.ProductQuery;
import com.dwd.user.pojo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/24 12:35
 * @Version 1.0
 */

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private IPetService petService;


    @PatchMapping
    public PageBean<Pet> queryPagePet(@RequestBody PetQuery petQuery, HttpServletRequest httpRequest) {
        //得到当前登陆用户
        LoginInfo loginInfo = SystemContext.getLoginInfo(httpRequest);
        //根据logininfo查询当前用户是否有shopId，如果有就是店铺管理员或员工,没有就是管理员
        Long shopId = shopMapper.queryShopIdByLoginInfo(loginInfo);
        if (shopId != null) {
            petQuery.setShopId(shopId);
        }
        return petService.queryPage(petQuery);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return AjaxResult.me();
    }

    /**
     * 批量上架
     *
     * @param pets
     * @return
     */
    @PostMapping("/batchUp")
    public AjaxResult batchRemove(@RequestBody List<Pet> pets) {
        try {
            //批量上架
            petService.batchUp(pets);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 批量下架
     *
     * @param pets
     * @return
     */
    @PostMapping("/batchDown")
    public AjaxResult batchDown(@RequestBody List<Pet> pets) {
        try {
            //批量下架
            petService.batchDown(pets);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 增加宠物
     *
     * @param pet
     * @param httpServletRequest
     * @return
     */
    @PostMapping
    public AjaxResult addOrUpdatePet(@RequestBody Pet pet, HttpServletRequest httpServletRequest) {
        try {
            LoginInfo loginInfo = SystemContext.getLoginInfo(httpServletRequest);
            Long shopId = shopMapper.queryShopIdByLoginInfo(loginInfo);
            pet.setShop_id(shopId);
            petService.addOrUpdatePet(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }







    /**
     * 门户网站分页
     * @param petQuery
     * @return
     */
    @PostMapping("/queryHomePage")
    public PageBean<Pet> queryHomePage(@RequestBody PetQuery petQuery) {
        PageBean<Pet> productPageBean = petService.queryPage(petQuery);
        return productPageBean;
    }

    /**
     * 根据id查询产品详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet queryPetById(@PathVariable("id") Long id) {
        return petService.queryById(id);
    }

}
